import * as sccEngine from "../../release/SccEngine"
const app = getApp();

let videoSourceMap = new Map(); //拉流组件绑定的用户列表
// let audioSourceMap = new Map();

let userListVideoMap = new Map(); //加会的用户列表
// let userListAudioMap = new Map();

let videoFpsCount1 = 0;
let videoFpsCount2 = 0;
let videoFpsCount3 = 0;
let videoBitCount1 = 0;
let videoBitCount2 = 0;
let videoBitCount3 = 0;

var callbacker = {};

let remoteUid1 = "";
let sourceid1 = "";
let remoteUid2 = "";
let sourceid2 = "";
let remoteUid3 = "";
let sourceid3 = "";
let playerFill1 = false;
let playerFill2 = false;
let playerFill3 = false;

var isHide = false; //页面隐藏的标识
var isHideLeave = false; //页面隐藏导致退会的标识
Page({
  data: {
    userid: "",
    externaluserid: "",
    roomId: "",
    roomName: "",
    displayName: "",
    rtmpLinkPush: "",
    rtmpLinkSub1: "",
    rtmpLinkSub2: "",
    rtmpLinkSub3: "",
    otherAudioSrc: "",
    player1fit: "contain",
    player2fit: "contain",
    player3fit: "contain",
    userLeave1: true,
    userLeave2: true,
    userLeave3: true,
    hiddenForm: false,
    route: "transcoding",
    urlSet: true,
    webSevUrl: "https://scc2.knowle.cn/",
    // gwSevUrl:"",
    logHidden: true,
    pushLog: {},
    subLog1: {},
    subLog2: {},
    subLog3: {},
    clickFlag: "false",
    mute: false,
    hiddenModal: true,
    btnHidden: false,
    // routeFlag: false,
    routeFlag: true,
    autoplay: true,
    debug: false,
    enableCamera: true,
    otherAudioHidden: true
  },
  onError: function (e) {
    console.log(e);
  },
  radioChange: function (e) {
    this.setData({
      route: e.detail.value
    })
  },
  setUrl: function () {
    this.setData({
      urlSet: false
    })
  },
  //取消按钮  
  modalCancel: function () {
    this.dialog.hideDialog();
  },
  //确认  
  modalConfirm: function (e) {
    this.dialog.hideDialog();
  },
  webUrlInput: function (e) {
    this.setData({
      webSevUrl: e.detail.value
    })
  },
  formSubmit: function (e) {
    console.log(e.detail.value);
    var formData = e.detail.value;
    sccEngine.setOption("webSevUrl", this.data.webSevUrl);
    // sccEngine.setOption("logServerUrl","");
    sccEngine.setOption("route", this.data.route);
    let userData = {};
    userData.test = "1";
    sccEngine.joinRoom(app.globalData.tangqiaoConfig.token, formData.roomid, 0, this.data.displayName, JSON.stringify(userData));

    wx.setStorage({
      key: 'roomName',
      data: formData.roomid,
    })
    wx.setStorage({
      key: 'displayName',
      data: formData.displayname,
    })
    wx.setStorage({
      key: 'webSevUrl',
      data: this.data.webSevUrl,
    })
    wx.setStorage({
      key: 'route',
      data: this.data.route,
    })
    wx.showLoading({
      title: '加会中',
    })
  },
  autoLogin: function (e) {
    sccEngine.setOption("webSevUrl", this.data.webSevUrl);
    // sccEngine.setOption("logServerUrl","");
    sccEngine.setOption("route", this.data.route);
    var formData = this.data
    let userData = {};
    userData.test = "1";
    sccEngine.joinRoom(app.globalData.tangqiaoConfig.token, '', 0, formData.displayName, JSON.stringify(userData));
 
    wx.setStorage({
      key: 'roomName',
      data: formData.roomName,
    })
    wx.setStorage({
      key: 'displayName',
      data: formData.displayName,
    })
    wx.setStorage({
      key: 'webSevUrl',
      data: this.data.webSevUrl,
    })
    wx.setStorage({
      key: 'route',
      data: this.data.route,
    })
    wx.showLoading({
      title: '加会中',
    })
  },
  startVideo() {
    sccEngine.startVideo();
  },
  playerMute() {
    this.setData({
      mute: !this.data.mute,
    })
    if (this.data.mute) {
      sccEngine.stopAudio();
    } else {
      sccEngine.startAudio();
    }
  },
  deviceChange() {
    var that = this;
    this.pusher.switchCamera({
      success: function () {
        console.log("switchCamera success")
      }
    });

  },
  reload: function () {
    let that = this;
    that.setData({
      rtmpLinkPush: null,
    }, function () {
      that.pusher.stop();
    });

    that.setData({
      player1fit: "contain",
      rtmpLinkSub1: null,
      userLeave1: true,
    }, function () {
      that.player1.stop();
    });

    that.setData({
      player2fit: "contain",
      rtmpLinkSub2: null,
      userLeave2: true
    }, function () {
      that.player2.stop();
    });

    that.setData({
      player3fit: "contain",
      rtmpLinkSub3: null,
      userLeave3: true
    }, function () {
      that.player3.stop();
    });

    that.setData({
      otherAudioSrc: null,
    }, function () {
      that.otherAudioPlayer.stop();
    });



    if (!isHide) {
      that.setData({
        hiddenForm: false,
      })
    }
    that.setData({
      mute: false,
      btnHidden: false,
      enableCamera: true
    });
    videoSourceMap.clear();
    // audioSourceMap.clear();
    userListVideoMap.clear();
    // userListAudioMap.clear();
    if (that.data.logHidden == false) {
      that.setData({
        logHidden: true
      })
    }
    that.memoryInput();
    that.dialog = that.selectComponent("#dialog");
  },
  leave() {
    console.log("function leave");
    sccEngine.leaveRoom();
    this.reload();
    // if (sub1Check) {
    //   clearTimeout(sub1Check);
    // }
  },
  onLoad(options) {
    callbacker = {
      sdkInitSuccess: this.sdkInitSuccess,
      sdkInitFail: this.sdkInitFail,
      joinRoomSuccess: this.joinRoomSuccess,
      joinRoomFail: this.joinRoomFail,
      userVideoStart: this.userVideoStart,
      userAudioStart: this.userAudioStart,
      userVideoStop: this.userVideoStop,
      userAudioStop: this.userAudioStop,
      startVideoSuccess: this.startVideoSuccess,
      startAudioSuccess: this.startAudioSuccess,
      stopVideoSuccess: this.stopVideoSuccess,
      stopAudioSuceess: this.stopAudioSuccess,
      subscribeVideoSuccess: this.subscribeVideoSuccess,
      unsubscribeVideoSuccess: this.unsubscribeVideoSuccess,
      unsubscribeAudioSuccess: this.unsubscribeAudioSuccess,
      userJoin: this.userJoin,
      userLeave: this.userLeave,
      leaveRoom: this.leaveRoom,
    }
    wx.getSystemInfo({
      success(res) {
        console.log("小程序基础库版本：" + res.SDKVersion)
      }
    })



    // 初始化引擎
    sccEngine.initialize(app.globalData.tangqiaoConfig.appKey, callbacker);
    this.dialog = this.selectComponent("#dialog");

    // 入参拿到roomName，
    if (options.roomName && options.displayName) {
      this.setData({
        // roomName: options.roomName,
        displayName: options.displayName
      })
      this.autoLogin()

    } else {
      this.memoryInput();
    }
  },
  onReady() {
    this.pusher = wx.createLivePusherContext('pusher');
    this.player1 = wx.createLivePlayerContext('player1');
    this.player2 = wx.createLivePlayerContext('player2');
    this.player3 = wx.createLivePlayerContext('player3');
    this.otherAudioPlayer = wx.createLivePlayerContext('otherAudioPlayer');
  },
  onHide() {
    console.log("onHide");
    isHide = true;
    sccEngine.leaveRoom();
    this.reload();
  },
  onShow() {
    wx.setKeepScreenOn({
      keepScreenOn: true,
    })
    if (isHide && isHideLeave && this.data.hiddenForm) {
      sccEngine.setOption("webSevUrl", this.data.webSevUrl);
      // sccEngine.setOption("gwSevUrl", this.data.gwSevUrl);
      sccEngine.setOption("route", this.data.route);
      let userData = {};
      userData.test = "1";
      sccEngine.joinRoom(app.globalData.tangqiaoConfig.token, '', 0, this.data.displayName, JSON.stringify(userData));
      wx.showLoading({
        title: '重新加会中',
      })
    }
    isHide = false;
  },
  memoryInput: function () {
    var that = this;

    wx.getStorage({
      key: 'webSevUrl',
      success: function (res) {
        that.setData({
          webSevUrl: res.data
        })
      },
    }),
      wx.getStorage({
        key: 'roomName',
        success: function (res) {
          that.setData({
            roomName: res.data
          })
        },
      }),
      wx.getStorage({
        key: 'displayName',
        success: function (res) {
          that.setData({
            displayName: res.data
          })
        },
      }),
      wx.getStorage({
        key: 'route',
        success: function (res) {
          that.setData({
            route: res.data
          })
          if (res.data == 'transcoding') {
            that.setData({
              routeFlag: true
            })
          } else {
            that.setData({
              routeFlag: false
            })
          }
        },
      })
  },
  onUnload() {
    console.log("onUnload");
    this.leave();
  },
  changeBtn: function () {
    this.setData({
      btnHidden: !this.data.btnHidden
    })
  },
  sdkInitSuccess: function (version) {
    console.log("webScc sdkInitSuccess:version-" + version);
    this.version = version;
    this.sdkInited = true;
  },
  sdkInitFail: function (code) {
    console.log("webScc sdkInitFail:code-" + code);
    if (code == 10001) {
      sccEngine.setOption("callback", callbacker);
    } else {
      wx.showModal({
        content: "初始化失败(" + code + ")",
        showCancel: false,
        confirmColor: "#298bfc",
        complete: wx.hideLoading()
      })
    }
  },
  joinRoomSuccess: function (_roomId, _externaluserid, _uid) {
    console.log("webScc joinRoomSuccess:_roomId-" + _roomId + ",_uid-" + _uid + ",_externaluserid-" + _externaluserid);
    isHideLeave = false;
    wx.hideLoading();
    this.setData({
      hiddenForm: true,
      roomId: _roomId
    })
    console.log("面侃ID：" + this.data.roomId)
    sccEngine.startVideo();
    sccEngine.startAudio();
    sccEngine.subscribeAudio("0", "audio_mixer", "1");
  },
  joinRoomFail: function (_code) {
    console.log("webScc joinRoomFail:_code-" + _code);
    wx.hideLoading();
    wx.showModal({
      content: "加会失败(" + _code + ")",
      showCancel: false,
      confirmColor: "#298bfc",
      complete: wx.hideLoading()
    })
  },
  userVideoStart: function (_userid, _sourceid) {
    let that = this;
    console.log('webScc userVideoStart:_userid-' + _userid + ",_sourceID-" + _sourceid);
    userListVideoMap.set(_userid, {
      "sourceid": _sourceid,
      "join": 0,
    });
    for (var i = 0; i < 3; i++) {
      console.log(("rtmpLinkSub" + (i + 1)) + "videoSource:" + that.data["rtmpLinkSub" + (i + 1)]);
      if (!videoSourceMap.get("rtmpLinkSub" + (i + 1))) {
        videoSourceMap.set("rtmpLinkSub" + (i + 1), {
          "userid": _userid,
          "sourceid": _sourceid
        });
        userListVideoMap.get(_userid).join = 1;
        sccEngine.subscribeVideo(_userid, _sourceid, "2");
        break;
      }
    }
  },
  userAudioStart: function (_userid, _sourceid) {
    // let that = this;
    console.log('webScc userAudioStart:_userid-' + _userid + ",_sourceID-" + _sourceid);
    sccEngine.subscribeAudio(_userid, _sourceid);
    // userListAudioMap.set(_userid, {
    //   "sourceid": _sourceid,
    //   "join": 0,
    // });
    // for (var i = 0; i < 3; i++) {
    //   console.log(("rtmpLinkSub" + (i + 1)) + "audioSource:" + that.data["rtmpLinkSub" + (i + 1)]);
    //   if (!audioSourceMap.get("rtmpLinkSub" + (i + 1))) {
    //     audioSourceMap.set("rtmpLinkSub" + (i + 1), {
    //       "userid": _userid,
    //       "sourceid": _sourceid
    //     });
    //     userListAudioMap.get(_userid).join = 1;
    //     sccEngine.subscribeAudio(_userid, _sourceid);
    //     break;
    //   } else {
    //     let audioTempSource = audioSourceMap.get("rtmpLinkSub" + (i + 1));
    //     sccEngine.subscribeAudio(audioTempSource.userid, audioTempSource.sourceid);
    //   }
    // }
  },
  userVideoStop: function (_userid, _sourceid) {
    console.log("webScc userVideoStop:_userid-" + _userid + ",_sourceid-" + _sourceid);
    let that = this;
    for (var [key, value] of videoSourceMap) { // 遍历Array
      if (value.userid == _userid && value.sourceid == _sourceid) {
        switch (key) {
          case "rtmpLinkSub1":
            that.setData({
              rtmpLinkSub1: null
            }, function () {
              that.player1.stop();
            });
            break;
          case "rtmpLinkSub2":
            that.setData({
              rtmpLinkSub2: null
            }, function () {
              that.player2.stop();
            });
            break;
          case "rtmpLinkSub3":
            that.setData({
              rtmpLinkSub3: null
            }, function () {
              that.player3.stop();
            });
            break;
        }
        break;
      }
    }
  },
  userAudioStop: function (_userid, _sourceid) {
    console.log("webScc userAudioStop:_userid-" + _userid + ",_sourceid-" + _sourceid);
  },
  startVideoSuccess: function (_rtmpLinkPush) {
    console.log("webScc startVideoSuccess:_rtmpLinkPush:" + _rtmpLinkPush);
    let that = this;
    that.setData({
      rtmpLinkPush: _rtmpLinkPush,
    }, function () {
      that.pusher.start()
    });
  },
  startAudioSuccess: function () {
    console.log("webScc startAudioSuccess");
  },
  stopVideoSuccess: function (_userid, _sourceid) {
    console.log("webScc stopVideoSuccess:_userid-" + _userid + ",_sourceid-" + _sourceid);
  },
  stopAudioSuccess: function (_userid, _sourceid) {
    console.log("webScc stopAudioSuccess:_userid-" + _userid + ",_sourceid-" + _sourceid);
    // this.pusher.stop();
  },
  subscribeVideoSuccess: function (_userid, _sourceid, _rtmpLinkSub) {
    console.log("webScc subscribeVideoSuccess:_userid-" + _userid + ",_sourceid-" + _sourceid + ",_rtmpLinkSub-" + _rtmpLinkSub);
    let that = this;
    console.log("webScc subscribeVideoSuccess videoSourceMap")
    console.log(videoSourceMap);
    for (var [key, value] of videoSourceMap) {
      if (value.userid == _userid && value.sourceid == _sourceid) {
        switch (key) {
          case "rtmpLinkSub1":
            that.setData({
              player1fit: "contain",
              rtmpLinkSub1: _rtmpLinkSub,
              userLeave1: false
            }, function () {
              that.player1.play();
            });
            break;
          case "rtmpLinkSub2":
            that.setData({
              player2fit: "contain",
              rtmpLinkSub2: _rtmpLinkSub,
              userLeave2: false
            }, function () {
              that.player2.play();
            });
            break;
          case "rtmpLinkSub3":
            that.setData({
              player3fit: "contain",
              rtmpLinkSub3: _rtmpLinkSub,
              userLeave3: false
            }, function () {
              that.player3.play();
            });
            break;
        }
        break;
      }

    }
    if (_userid == 0) {
      that.setData({
        otherAudioSrc: _rtmpLinkSub,
      }, function () {
        that.otherAudioPlayer.play();
      });
    }
  },
  unsubscribeVideoSuccess: function (_userid, _sourceid) {
    console.log("webScc unsubscribeVideoSuccess:_userid-" + _userid + ",_sourceid-" + _sourceid);
    let that = this;
    for (var [key, value] of videoSourceMap) {
      if (value.userid == _userid && value.sourceid == _sourceid) {
        switch (key) {
          case "rtmpLinkSub1":
            that.setData({
              player1fit: "contain",
              rtmpLinkSub1: null
            },
              function () {
                that.player1.stop();
              }
            );
            playerFill1 = false;
            break;
          case "rtmpLinkSub2":
            that.setData({
              player2fit: "contain",
              rtmpLinkSub2: null
            },
              function () {
                that.player2.stop();
              }
            );
            playerFill2 = false;
            break;
          case "rtmpLinkSub3":
            that.setData({
              player3fit: "contain",
              rtmpLinkSub3: null
            },
              function () {
                that.player3.stop();
              }
            );
            playerFill3 = false;
            break;
        }
        videoSourceMap.delete(key);
        // audioSourceMap.delete(key);
        break;
      }
    }
  },
  unsubscribeAudioSuccess: function (_userid, _sourceid) {
    console.log("webScc unsubscribeAudioSuccess:_userid-" + _userid + ",_sourceid-" + _sourceid);
  },
  userJoin: function (_userid, _userDisplayName, _userData) {
    console.log("webScc userJoin:_userid-" + _userid + ",_userDisplayName-" + _userDisplayName);
    console.log(_userData);
  },
  userLeave: function (_userid) {
    console.log("webScc userLeave:_userid-" + _userid);
    userListVideoMap.delete(_userid);
    // userListAudioMap.delete(_userid);
    let that = this;
    for (var [key, value] of videoSourceMap) { // 遍历Array
      if (value.userid == _userid) {
        videoSourceMap.delete(key);
        // audioSourceMap.delete(key);
        switch (key) {
          case "rtmpLinkSub1":
            that.setData({
              rtmpLinkSub1: null,
              userLeave1: true,
            }, function () {
              that.player1.stop();
            });
            playerFill1 = false;
            break;
          case "rtmpLinkSub2":
            that.setData({
              rtmpLinkSub2: null,
              userLeave2: true,
            }, function () {
              that.player2.stop();
            });
            playerFill2 = false;
            break;
          case "rtmpLinkSub3":
            that.setData({
              rtmpLinkSub3: null,
              userLeave3: true,
            }, function () {
              that.player3.stop();
            });
            playerFill3 = false;
            break;
        }
      }
    }
    that.reSort();

  },

  leaveRoom: function (_code) {
    console.log("webScc leaveRoom:_code-" + _code);
    if (isHide) {
      isHideLeave = true;
    } else {
      if (_code != 0) {
        wx.showModal({
          content: "退会(" + _code + ")",
          showCancel: false,
          confirmColor: "#298bfc",
          complete: wx.hideLoading()
        })
      }
      isHideLeave = false;
    }
    this.reload();
  },
  netStatusChange(info) {
    info = info.detail.info;
    console.log('netStatusChange');
    console.log(info);
    info.videoFPS = Math.round(info.videoFPS);
    this.setData({
      pushLog: info
    })
    sccEngine.getPublishLog(info);
  },
  netPlayer1StatusChange(info) {
    info = info.detail.info;
    console.log('player 1 StatusChange');
    console.log(info);
    var that = this;
    that.setData({
      subLog1: info
    })
    if (playerFill1) {
      if (info.videoWidth > info.videoHeight) {
        that.setData({
          player1fit: "contain"
        })
      } else {
        that.setData({
          player1fit: "fillCrop"
        })
      }
    }


    if (videoSourceMap.get("rtmpLinkSub1")) {
      var videoSource = videoSourceMap.get("rtmpLinkSub1");
      remoteUid1 = videoSource.userid;
      sourceid1 = videoSource.sourceid;
    }

    sccEngine.getSubscribelog(remoteUid1, info);
    // if (info.videoBitrate == 0) {
    //   videoBitCount1++;
    //   if (videoBitCount1 >= 6) {
    //     videoBitCount1 = 0;
    //     that.reSubScribe(remoteUid1, sourceid1);
    //   }
    // } else {
    //   videoBitCount1 = 0;
    // }
    // if (info.videoFPS <= 1) {
    //   videoFpsCount1++;
    //   if (videoFpsCount1 >= 4) {
    //     videoFpsCount1 = 0;
    //     that.reSubScribe(remoteUid1, sourceid1);
    //   }
    // } else {
    //   videoFpsCount1 = 0;
    // }
  },
  netPlayer2StatusChange(info) {
    info = info.detail.info;
    console.log('player 2 StatusChange');
    console.log(info);
    var that = this;
    if (playerFill2) {
      if (info.videoWidth > info.videoHeight) {
        that.setData({
          player2fit: "contain"
        })
      } else {
        that.setData({
          player2fit: "fillCrop"
        })
      }
    }
    that.setData({
      subLog2: info
    })
    if (videoSourceMap.get("rtmpLinkSub2")) {
      var videoSource = videoSourceMap.get("rtmpLinkSub2");
      remoteUid2 = videoSource.userid;
      sourceid2 = videoSource.sourceid;
    }

    sccEngine.getSubscribelog(remoteUid2, info);
    // if (info.videoBitrate == 0) {
    //   videoBitCount2++;
    //   if (videoBitCount2 >= 6) {
    //     videoBitCount2 = 0;
    //     that.reSubScribe(remoteUid2, sourceid2);
    //   }
    // } else {
    //   videoBitCount2 = 0;
    // }
    // if (info.videoFPS <= 1) {
    //   videoFpsCount2++;
    //   if (videoFpsCount2 >= 4) {
    //     videoFpsCount2 = 0;
    //     that.reSubScribe(remoteUid2, sourceid2);
    //   }
    // } else {
    //   videoFpsCount2 = 0;
    // }
  },
  netPlayer3StatusChange(info) {
    info = info.detail.info;
    console.log('player 3 StatusChange');
    console.log(info);
    var that = this;
    if (playerFill3) {
      if (info.videoWidth > info.videoHeight) {
        that.setData({
          player3fit: "contain"
        })
      } else {
        that.setData({
          player3fit: "fillCrop"
        })
      }
    }
    that.setData({
      subLog3: info
    })
    if (videoSourceMap.get("rtmpLinkSub3")) {
      var videoSource = videoSourceMap.get("rtmpLinkSub3");
      remoteUid3 = videoSource.userid;
      sourceid3 = videoSource.sourceid;
    }

    sccEngine.getSubscribelog(remoteUid3, info);
    // if (info.videoBitrate == 0) {
    //   videoBitCount3++;
    //   if (videoBitCount3 >= 6) {
    //     videoBitCount3 = 0;
    //     that.reSubScribe(remoteUid3, sourceid3);
    //   }
    // } else {
    //   videoBitCount3 = 0;
    // }
    // if (info.videoFPS <= 1) {
    //   videoFpsCount3++;
    //   if (videoFpsCount3 >= 4) {
    //     videoFpsCount3 = 0;
    //     that.reSubScribe(remoteUid3, sourceid3);
    //   }
    // } else {
    //   videoFpsCount3 = 0;
    // }
  },
  pushStatechange(e) {
    console.log('live-pusher code:' + e.detail.code + ',message:' + e.detail.message);
    sccEngine.getStateChange("push", e.detail.code, e.detail.message);
    if (e.detail.code == "-1307") {
      console.log("重新推流");
      sccEngine.stopVideo();
      sccEngine.startVideo();
      this.pusher.start();
    }
  },
  otherstatechange(e) {
    console.log('otherplayer code:' + e.detail.code + ',message:' + e.detail.message);
    var that = this;
    // sccEngine.getStateChange(0, e.detail.code, e.detail.message);
    if (e.detail.code == "-2301") {
      console.log("otherplayer重新拉流");
      that.setData({
        otherAudioSrc: that.data.otherAudioSrc,
      }, function () {
        that.otherAudioPlayer.play();
      });
    }
  },
  player1Statechange(e) {
    console.log('live-player1 code:' + e.detail.code + ',message:' + e.detail.message);
    sccEngine.getStateChange(remoteUid1, e.detail.code, e.detail.message);
    if (e.detail.code == "-2301") {
      console.log("player1重新拉流");
      this.reSubScribe(remoteUid1, sourceid1);
    }
    if (e.detail.code == "2009") {
      playerFill1 = true;
      // this.player1.play();
    }

  },
  player2Statechange(e) {
    console.log('live-player2 code:' + e.detail.code + ',message:' + e.detail.message);
    sccEngine.getStateChange(remoteUid2, e.detail.code, e.detail.message);
    if (e.detail.code == "-2301") {
      console.log("player2重新拉流");
      this.reSubScribe(remoteUid2, sourceid2);
    }
    if (e.detail.code == "2009") {
      playerFill2 = true;
    }
  },
  player3Statechange(e) {
    console.log('live-player3 code:' + e.detail.code + ',message:' + e.detail.message);
    sccEngine.getStateChange(remoteUid3, e.detail.code, e.detail.message);
    if (e.detail.code == "-2301") {
      console.log("player3重新拉流");
      this.reSubScribe(remoteUid3, sourceid3);
    }
    if (e.detail.code == "2009") {
      playerFill3 = true;
    }
  },

  showLog: function (e) {
    this.setData({
      // logHidden: !this.data.logHidden,
      debug: !this.data.debug
    })
  },
  switchVideo: function () {
    var that = this;
    that.setData({
      enableCamera: !that.data.enableCamera
    })
    if (that.data.enableCamera) {
      wx.sccEngine.startVideo();
    } else {
      wx.sccEngine.stopVideo();
      that.setData({
        rtmpLinkPush: ""
      }, function () {
        that.pusher.stop();
      })
      wx.sccEngine.startVideo();
      that.pusher.start();
      wx.sccEngine.stopVideo();
    }
  },
  showDialog() {
    this.dialog.showDialog();
  },
  reSubScribe: function (remoteUid, sourceid) {
    console.log("webScc reSubScribe: remoteUid -" + remoteUid + "sourceid-" + sourceid);

    sccEngine.subscribeVideo(remoteUid, sourceid, "2");
    sccEngine.subscribeAudio(remoteUid, sourceid);
  },
  reSort: function () {
    console.log("webScc reSort");
    var that = this;
    if (that.data.rtmpLinkSub1 == null && that.data.rtmpLinkSub2 != null) {
      if (videoSourceMap.get("rtmpLinkSub2")) {
        videoSourceMap.set("rtmpLinkSub1", {
          "userid": videoSourceMap.get("rtmpLinkSub2").userid,
          "sourceid": videoSourceMap.get("rtmpLinkSub2").sourceid
        });
        that.setData({
          rtmpLinkSub1: that.data.rtmpLinkSub2,
          userLeave1: false,
        }, function () {
          that.player1.play();
          that.player2.stop();
        }),
          that.setData({
            rtmpLinkSub2: null,
            userLeave2: true,
          }, function () {
            that.player2.stop();
          }),
          videoSourceMap.delete("rtmpLinkSub2");
        // audioSourceMap.delete("rtmpLinkSub2");
        console.log(videoSourceMap);
        if (that.data.rtmpLinkSub2 == null && that.data.rtmpLinkSub3 != null) {
          if (videoSourceMap.get("rtmpLinkSub3")) {
            videoSourceMap.set("rtmpLinkSub2", {
              "userid": videoSourceMap.get("rtmpLinkSub3").userid,
              "sourceid": videoSourceMap.get("rtmpLinkSub3").sourceid
            });
            that.setData({
              rtmpLinkSub2: that.data.rtmpLinkSub3,
              userLeave2: false
            }, function () {
              that.player2.play();
              that.player3.stop();
            }),
              that.setData({
                rtmpLinkSub3: null,
                userLeave3: true,
              }, function () {
                that.player3.stop();
              })
            videoSourceMap.delete("rtmpLinkSub3");
            // audioSourceMap.delete("rtmpLinkSub3");
            console.log(videoSourceMap);
          }
        }
      }
    }
    if (that.data.rtmpLinkSub2 == null && that.data.rtmpLinkSub3 != null) {
      if (videoSourceMap.get("rtmpLinkSub3")) {
        videoSourceMap.set("rtmpLinkSub2", {
          "userid": videoSourceMap.get("rtmpLinkSub3").userid,
          "sourceid": videoSourceMap.get("rtmpLinkSub3").sourceid
        });
        that.setData({
          rtmpLinkSub2: that.data.rtmpLinkSub3,
          userLeave2: false
        }, function () {
          that.player2.play();
          that.player3.stop();
        }),
          that.setData({
            rtmpLinkSub3: null,
            userLeave3: true,
          }, function () {
            that.player3.stop();
          })
        videoSourceMap.delete("rtmpLinkSub3");
        // audioSourceMap.delete("rtmpLinkSub3");
        console.log(videoSourceMap);
      }
    }
    //用户列表中没有拉流的需要补上
    for (var [key, value] of userListVideoMap) {
      if (value.join == 0) {
        that.otherVideo(key, value.sourceid);
      }
    }
    // for (var [key, value] of userListAudioMap) {
    //   if (value.join == 0) {
    //     // that.otherAudio(key, value.sourceid);
    //   }
    // }
  },
  otherVideo: function (_userid, _sourceid) {
    for (var i = 0; i < 3; i++) {
      if (!videoSourceMap.get("rtmpLinkSub" + (i + 1))) {
        videoSourceMap.set("rtmpLinkSub" + (i + 1), {
          "userid": _userid,
          "sourceid": _sourceid
        });
        userListVideoMap.get(_userid).join = 1;
        sccEngine.subscribeVideo(_userid, _sourceid, "2");
        break;
      }
    }
  },
  // otherAudio: function(_userid, _sourceid) {
  //   for (var i = 0; i < 3; i++) {
  //     if (!audioSourceMap.get("rtmpLinkSub" + (i + 1))) {
  //       audioSourceMap.set("rtmpLinkSub" + (i + 1), {
  //         "userid": _userid,
  //         "sourceid": _sourceid
  //       });
  //       userListAudioMap.get(_userid).join = 1;
  //       sccEngine.subscribeAudio(_userid, _sourceid);
  //       break;
  //     }
  //   }
  // }

})